package com.osce.go_score.service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.csce.domain.entity.go_score.GoScoreExam;
import com.csce.domain.entity.go_score.GoScoreExamPlace;
import com.osce.common.utils.DateUtils;
import com.osce.common.utils.SecurityUtils;
import com.osce.go_score.mapper.GoScoreExamMapper;
import com.osce.interfaces.go_score.IGoScoreExExamMarkService;
import com.osce.interfaces.go_score.IGoScoreExamPlaceService;
import com.osce.interfaces.go_score.IGoScoreExamService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * 妇产科考试Service业务层处理
 *
 * @author zzx
 * @since 2024-05-17
 */
@Service
public class GoScoreExamServiceImpl extends ServiceImpl<GoScoreExamMapper, GoScoreExam> implements IGoScoreExamService {

    @Resource private IGoScoreExamPlaceService goScoreExamPlaceService;
    @Resource private IGoScoreExExamMarkService goScoreExExamMarkService;


    /**
     * 查询妇产科考试
     *
     * @param id 妇产科考试主键
     * @return 妇产科考试
     */
    @Override
    public GoScoreExam selectGoScoreExamById(Long id) {
        GoScoreExam goScoreExam = baseMapper.selectGoScoreExamById(id);
        List<GoScoreExamPlace> goScoreExamPlaceList = goScoreExamPlaceService.selectGoScoreExamPlaceByExamId(id);
        goScoreExam.setGoScoreExamPlaceList(goScoreExamPlaceList);
        return goScoreExam;
    }

    /**
     * 查询妇产科考试列表
     *
     * @param goScoreExam 妇产科考试
     * @return 妇产科考试
     */
    @Override
    public List<GoScoreExam> selectGoScoreExamList(GoScoreExam goScoreExam) {
        return baseMapper.selectGoScoreExamList(goScoreExam);
    }

    /**
     * 新增妇产科考试
     *
     * @param goScoreExam 妇产科考试
     * @return 结果
     */
    @Transactional
    @Override
    public int insertGoScoreExam(GoScoreExam goScoreExam) {
        goScoreExam.setCreateTime(DateUtils.getNowDate());
        goScoreExam.setCreateBy(SecurityUtils.getUserIdStr());
        int rows = baseMapper.insertGoScoreExam(goScoreExam);
        goScoreExamPlaceService.batchInsertGoScoreExamPlace(goScoreExam.getId(),goScoreExam.getGoScoreExamPlaceList());
        return rows;
    }

    /**
     * 修改妇产科考试
     *
     * @param goScoreExam 妇产科考试
     * @return 结果
     */
    @Transactional
    @Override
    public int updateGoScoreExam(GoScoreExam goScoreExam) {
        goScoreExamPlaceService.deleteGoScoreExamPlaceByExamId(goScoreExam.getId());
        goScoreExamPlaceService.batchInsertGoScoreExamPlace(goScoreExam.getId(),goScoreExam.getGoScoreExamPlaceList());
        return baseMapper.updateGoScoreExam(goScoreExam);
    }

    /**
     * 批量删除妇产科考试
     *
     * @param ids 需要删除的妇产科考试主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteGoScoreExamByIds(Long[] ids) {
        goScoreExamPlaceService.deleteGoScoreExamPlaceByExamIds(ids);
        //删除成绩
        goScoreExExamMarkService.deleteGoScoreExExamMarkByExamIds(ids);
        return baseMapper.deleteGoScoreExamByIds(ids);
    }

    /**
     * 删除妇产科考试信息
     *
     * @param id 妇产科考试主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteGoScoreExamById(Long id) {
        goScoreExamPlaceService.deleteGoScoreExamPlaceByExamId(id);
        //删除成绩
        goScoreExExamMarkService.deleteGoScoreExExamMarkByExamId(id);
        return baseMapper.deleteGoScoreExamById(id);
    }
}
